package com.example.fenye.service.forum.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.example.fenye.dao.forum.ForumUserDao;
import com.example.fenye.entity.forum.ForumUser;
import com.example.fenye.service.forum.ForumUserService;
import com.example.fenye.util.MD5Utils;

@Service
public class ForumUserServiceImpl implements ForumUserService {

	@Resource
	ForumUserDao forumUserDao;
	
	@Override
	public Long addUser(ForumUser user) {
		Long userId = forumUserDao.insertUser(user);
		return userId;
	}

	@Override
	public boolean validateUser(String name, String password) {
		String passInDb = forumUserDao.getPassByName(name);
		if(passInDb!=null && passInDb.equals(password)){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public ForumUser getForumUserByNamePass(String name, String password) {
		ForumUser forumUserByName = forumUserDao.getUserByName(name);
		String salt = forumUserByName.getUserSalt();
		String addedSaltPassword = MD5Utils.MD5Encode(password + salt,"utf-8"); 
		
		ForumUser froumUser = forumUserDao.getUserByNamePass(name, addedSaltPassword);
		return froumUser;
	}

}
